package sbt.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  @FindBy(css = "nav a[href $= '?logout']")
  private WebElement logoutLink;
  
  @FindBy(xpath = ".//*[@class='button']//img[@title='Add movie']")
  private WebElement addNewFilmLink;
  
  @FindBy(id = "q")
  private WebElement searchField;
  
  
  
  public void clickSearchedFilm(String title) {
	  driver.findElement(By.xpath("//div[@id='results']//div[starts-with(@title,'"+title+"')]")).click();
	
  }

  
  public InternalPage setFilmTitleField(String text) {
	  searchField.clear();
	  searchField.sendKeys(text);
	  return this;
  }
  
  public InternalPage enterSearchField(){
	  searchField.sendKeys(Keys.RETURN);
	  return this;
  }
  
  public InternalPage ensureFilmSearched(String title) {
	  wait.until(presenceOfElementLocated(By.xpath("//div[@id='results']//div[starts-with(@title,'"+title+"')]")));
	  return this;
  }
  
  public InternalPage ensureFilmNotSearched() {
	  wait.until(presenceOfElementLocated(By.xpath(".//*[@id='results']/div[text()='No movies where found.']")));
	  return this;
  }
    
  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public AddNewFilmPage clickAddNewFilmLink() {
	  addNewFilmLink.click();
	  return pages.addNewFilmPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }
}
